﻿using LSL.SqlClient;
using System;
using System.Web;
using System.Web.SessionState;
using Liu_Project.Utils;
using System.Threading.Tasks;

namespace Liu_Project.Web.Admin
{
    public abstract class HandlerBaseAsync : HttpTaskAsyncHandler, IRequiresSessionState
    {
        #region IHttpHandler 成员
        public override bool IsReusable => throw new NotImplementedException();
        #endregion

#pragma warning disable S1104 // Fields should not have public accessibility
        public HttpContext _context;
        public int page = 1;
        public int pageSize = 10;
        public int total = 0;
        protected string _logOperation = string.Empty;
#pragma warning restore S1104 // Fields should not have public accessibility

        /// <summary>
        /// 获取用户信息,返回NULL则SESSION过期
        /// </summary>
        public Entity.Admin.EntUser CurrentUser
        {
            get
            {
                var currentUser = HttpContext.Current.Session["CurrentUser"];
                if (currentUser != null)
                {
                    return (Entity.Admin.EntUser)currentUser;
                }
                else
                {
                    return null;
                }
            }
        }

        public async override Task ProcessRequestAsync(HttpContext context)
        {
            if (context.Session["CurrentUser"] == null)
            {
                context.Response.Write(CommonFunction.ErrorJson("用户信息过期，请重新登录!", 101));
                context.Response.End();
                return;
            }

            string action = DataConvert.ToString(context.Request["act"], "");
            if (string.IsNullOrWhiteSpace(action))
            {
                context.Response.Write(CommonFunction.ErrorJson("方法名不能为空!"));
                context.Response.End();
                return;
            }

            DateTime requestBeginTime = DateTime.Now;
            _context = context;
            page = DataConvert.ToInt(context.Request["page"]);
            pageSize = DataConvert.ToInt(context.Request["limit"]);


            string errorMessage = string.Empty;
            DateTime requestEndTime;
            Type ty = this.GetType();
            try
            {
                System.Reflection.MethodInfo method = ty.GetMethod(action);
                if (method != null)
                {
                    requestBeginTime = DateTime.Now;
                    method.Invoke(this, null);
                }
                else
                {
                    errorMessage = "没有找到对应的处理方法.";
                    _context.Response.Write(CommonFunction.ErrorJson(errorMessage));
                }
            }
            catch (Exception ex)
            {
                LogFunction.WriteLog(ty.Name + "-" + action, ex);
                errorMessage = ex.Message + "--" + ex.InnerException == null ? string.Empty : ex.InnerException.Message;
                _context.Response.Write(CommonFunction.ErrorJson(errorMessage));
            }
            finally
            {
                requestEndTime = DateTime.Now;
                var clientIp = IPHelper.GetClientIP();
                await LogDBHelper.LogDB_Insert_Async(_context, CurrentUser, requestBeginTime, requestEndTime, _logOperation, clientIp, errorMessage);
            }
        }

        public override void ProcessRequest(HttpContext context)
        {
            throw new NotImplementedException();
        }

    }
}